layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    var tableIns = table.render({
        elem: '#userList',
        url : 'user/userPage',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 10,
        limits : [10,15,20,25],
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'userUsername', title: '员工姓名', align:'center'},
            {field: 'userDate', title: '修改时间',align:'center'},
            {field: 'userRemark', title: '备注', align:'center'},
            {title: '操作', width:200, templet:'#newsListBar',fixed:"right",align:"center"},
            {field: '', width: 2}
        ]]
    });

    function addNews(edit){
        var index = layui.layer.open({
            title : "添加员工",
            type : 2,
            content : "user/AddOrUpdate/0",
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    $(".addNews_btn").click(function(){
        addNews();
    })

    function updateNews(edit){
        var index = layui.layer.open({
            title : "修改员工",
            type : 2,
            content : "user/AddOrUpdate/" + edit,
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            updateNews(data.userId);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此员工？',{icon:3, title:'提示信息'},function(index){
                $.post("user/delete",{id : data.userId},function(data){
                    if (data.success){
                        layer.msg(data.message);
                        tableIns.reload();
                        layer.close(index);
                    }else {
                        layer.msg(data.message);
                    }
                })
            });
        }
    });

})